﻿Imports System.Windows.Forms
Imports System.IO

Public Class CharacterGuild

    Private _id As Integer = -1
    Private _name As String
    Private _desc As String
    Private _initialClass As Integer = -1

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return _desc
        End Get
        Set(ByVal value As String)
            _desc = value
        End Set
    End Property

    Public Property InitialClassID() As Integer
        Get
            Return _initialClass
        End Get
        Set(ByVal value As Integer)
            _initialClass = value
        End Set
    End Property

    Public Function ToXElement() As XElement
        Try
            Dim guild = <guild id=<%= _id %> initialClass=<%= _initialClass %>>
                            <name><%= _name %></name>
                            <desc><%= _desc %></desc>
                        </guild>
            Return guild
        Catch ex As Exception
            Failed(ex.ToString)
            Return Nothing
        End Try

    End Function

    Public Sub LoadXML(ByVal guildXML As String)
        Dim sr As New StringReader(guildXML)
        Dim classX As XElement

        Try
            classX = XElement.Load(sr)
        Catch ex As Exception
            Failed(guildXML, ex.ToString)
            Exit Sub
        End Try
        Try
            _id = Integer.Parse(classX.@id)
            _initialClass = Integer.Parse(classX.@initialClass)
            _name = classX.<name>.Value
            _desc = classX.<desc>.Value
        Catch ex As Exception
            Failed(guildXML, ex.ToString)
        End Try
    End Sub

    Private Sub Failed(ByVal xmlString As String, ByVal ex As String)
        MessageBox.Show(xmlString + vbCrLf + vbCrLf + ex, "Failed to LoadXML in CharacterGuild", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Private Sub Failed(ByVal ex As String)
        MessageBox.Show(ex, "Failed in CharacterGuild", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Public ReadOnly Property IsValid() As Boolean
        Get
            Return CheckValidData()
        End Get
    End Property

    Private Function CheckValidData() As Boolean
        ' Check main details
        If _name = "" Or _desc = "" Then
            Return False
        End If
        If _initialClass = -1 Then
            Return False
        End If
        Return True
    End Function
End Class
